package com.hhkj.admin.conf.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.hhkj.admin.common.annotation.ClearRedis;
import com.hhkj.admin.common.annotation.UseRedis;
import com.hhkj.admin.common.event.DailyZeroHourTimeUpEvent;
import com.hhkj.admin.conf.common.cont.CommissionRateConst;
import com.hhkj.admin.conf.common.cont.ConfConst;
import com.hhkj.admin.conf.model.extend.CommissionRate;
import com.hhkj.admin.conf.domain.Conf;
import com.hhkj.admin.conf.model.dopo.ConfDo;
import com.hhkj.admin.conf.model.dopo.ConfPo;
import com.hhkj.admin.conf.model.req.CommissionRateReq;
import com.hhkj.admin.conf.model.res.CommissionRateRes;
import com.hhkj.admin.conf.service.base.impl.ConfServiceImpl;
import com.hhkj.admin.conf.service.biz.CommissionRateBizService;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CommissionRateBizServiceImpl implements CommissionRateBizService {

    @Autowired
    private ConfServiceImpl confService;

    @Autowired
    private CommissionRateBizService commissionRateBizService;

    @Override
    public ConfConst.Code getCode() {
        return ConfConst.Code.COMMISSION_RATE;
    }

    @Override
    @UseRedis(value = REDIS_CACHE_PREFIX, clazz = CommissionRateRes.class, timeout = TIMEOUT, timeUnit = TimeUnit.HOURS)
    public CommissionRateRes get() {
        CommissionRateRes commissionRateRes = new CommissionRateRes();
        //获取正在生效的规则
        ConfDo confDo = new ConfDo();
        confDo.setCode(ConfConst.Code.COMMISSION_RATE.getCode());
        confDo.setEffectiveCode(ConfConst.EffectiveCode.EFFECTIVE.getCode());
        ConfPo effectivePo = confService.getPoByDo(confDo);
        if(ObjectUtil.isEmpty(effectivePo)) {
            throw new GlobalException("没有生效的规则,请联系管理员");
        }
        CommissionRate effective = JSONUtil.toBean(effectivePo.getConf(), CommissionRate.class);
        commissionRateRes.setEffective(effective);

        //获取未生效的规则
        confDo.setEffectiveCode(ConfConst.EffectiveCode.WAITING.getCode());
        ConfPo waitingPo  = confService.getPoByDo(confDo);
        if (ObjectUtil.isNotEmpty(waitingPo)) {
            CommissionRate waiting = JSONUtil.toBean(waitingPo.getConf(), CommissionRate.class);
            commissionRateRes.setWaiting(waiting);
        }
        return commissionRateRes;
    }

    @Override
    @Transactional
    @ClearRedis(value = REDIS_CACHE_PREFIX, clazz = CommissionRateRes.class)
    public boolean update(CommissionRateReq commissionRateReq) {
        Conf conf = new Conf();
        conf.setCode(ConfConst.Code.COMMISSION_RATE.getCode());
        if (CommissionRateConst.ImmediateFlag.IMMEDIATE.getCode().equals(commissionRateReq.getImmediateFlag())) {
            //如果是立即神效
            confService.updateEffectiveCodeBatch(conf, ConfConst.EffectiveCode.INVALID);
            CommissionRate commissionRate = BeanUtil.toBean(commissionRateReq, CommissionRate.class);
            commissionRate.setEffectiveTime(new Date());
            String jsonStr = JSONUtil.toJsonStr(commissionRate);
            conf.setConf(jsonStr);
            conf.setEffectiveCode(ConfConst.EffectiveCode.EFFECTIVE.getCode());
        } else {
            //不是立即生效
            conf.setEffectiveCode(ConfConst.EffectiveCode.WAITING.getCode());
            confService.updateEffectiveCodeBatch(conf, ConfConst.EffectiveCode.INVALID);
            CommissionRate commissionRate = BeanUtil.toBean(commissionRateReq, CommissionRate.class);
            String jsonStr = JSONUtil.toJsonStr(commissionRate);
            conf.setConf(jsonStr);
            conf.setEffectiveCode(ConfConst.EffectiveCode.WAITING.getCode());
        }
        return confService.save(conf);
    }

    @Override
    @ClearRedis(value = REDIS_CACHE_PREFIX, clazz = CommissionRateRes.class)
    public boolean removeCommissionRate() {
        Conf conf = new Conf();
        conf.setCode(ConfConst.Code.COMMISSION_RATE.getCode());
        conf.setEffectiveCode(ConfConst.EffectiveCode.WAITING.getCode());
        return confService.updateEffectiveCodeBatch(conf, ConfConst.EffectiveCode.INVALID);
    }

    @EventListener
    @Order(1)
    public void checkAndActivateWaitingCommissionRate(DailyZeroHourTimeUpEvent dailyZeroHourTimeUpEvent) {
        List<String> taskList = (List) dailyZeroHourTimeUpEvent.getSource();
        if (taskList.contains("checkAndActivateWaitingCommissionRate") == false) {
            return ;
        }
        CommissionRateRes commissionRateRes = commissionRateBizService.get();
        CommissionRate waiting = commissionRateRes.getWaiting();
        if (ObjectUtil.isEmpty(waiting)) {
            return;
        }
        if (new Date().compareTo(waiting.getEffectiveTime()) >= 0) {
            commissionRateBizService.activateWaitingCommissionRate();
        }
    }
    @ClearRedis(value = REDIS_CACHE_PREFIX, clazz = CommissionRateRes.class)
    public void activateWaitingCommissionRate() {
        //原本有效的变成失效
        Conf conf = new Conf();
        conf.setCode(ConfConst.Code.COMMISSION_RATE.getCode());
        conf.setEffectiveCode(ConfConst.EffectiveCode.EFFECTIVE.getCode());
        confService.updateEffectiveCodeBatch(conf, ConfConst.EffectiveCode.INVALID);

        //原本待生效的生效
        conf.setCode(ConfConst.Code.COMMISSION_RATE.getCode());
        conf.setEffectiveCode(ConfConst.EffectiveCode.WAITING.getCode());
        confService.updateEffectiveCodeBatch(conf, ConfConst.EffectiveCode.EFFECTIVE);
    }
}
